// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "FPSGameMode.h"
#include "FPSHUD.h"
#include "FPSCharacter.h"
#include "FPSGameState.h"
#include "UObject/ConstructorHelpers.h"
#include "Kismet/GameplayStatics.h"


AFPSGameMode::AFPSGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/Blueprints/BP_Player"));
	DefaultPawnClass = PlayerPawnClassFinder.Class;

	// use our custom HUD class
	HUDClass = AFPSHUD::StaticClass();

	GameStateClass = AFPSGameState::StaticClass();
}

void AFPSGameMode::CompleteMission(APawn * InstigatorPawn, bool bMissionSuccess)
{
	UE_LOG(LogTemp, Warning, TEXT("aaaaaaaaaaaa"));
	if (InstigatorPawn)
	{
//		InstigatorPawn->DisableInput(nullptr);

		if (SpectatingViewpointClass)
		{
			TArray<AActor*>	ReturnedTarget;
			UGameplayStatics::GetAllActorsOfClass(this, SpectatingViewpointClass, ReturnedTarget);
			if (ReturnedTarget.Num() > 0)
			{
				AActor* NewViewTarget = ReturnedTarget[0];
				for (FConstPlayerControllerIterator it = GetWorld()->GetPlayerControllerIterator(); it; it++)
				{
					APlayerController* PC = it->Get();
					if (PC)
					{
						PC->SetViewTargetWithBlend(NewViewTarget, 0.5f, VTBlend_Cubic);
					}
				}
			//	APlayerController* PC = Cast<APlayerController>(InstigatorPawn->GetController());
			//	if (PC)
			//	{
			//		PC->SetViewTargetWithBlend(NewViewTarget, 0.5f, VTBlend_Cubic);
			//	}
			}
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("SpectatingViewpointClass is nullptr"));
		}
	}
	
	AFPSGameState* GS = GetGameState<AFPSGameState>();
	if (GS)
	{
		UE_LOG(LogTemp, Warning, TEXT("get FPSGameState"));
		GS->MulticastOnMissionComplete(InstigatorPawn, bMissionSuccess);
	}

//	OnCompleteMission(InstigatorPawn, bMissionSuccess);
}
